package com.example.administrator.appstpremvpdemo.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.appstpremvpdemo.R;
import com.example.administrator.appstpremvpdemo.common.Constances;
import com.example.administrator.appstpremvpdemo.contract.AppListContract;
import com.example.administrator.appstpremvpdemo.model.entity.Datebean;
import com.example.administrator.appstpremvpdemo.model.entity.ValueBean;
import com.example.administrator.appstpremvpdemo.presenter.TaskListPresenter;
import com.example.administrator.appstpremvpdemo.view.activity.AppDetailActivity;
import com.example.administrator.appstpremvpdemo.view.activity.BaseActivity;
import com.example.administrator.appstpremvpdemo.wegit.CommonAdapter;
import com.example.administrator.appstpremvpdemo.wegit.PageStateLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;


/**
 * Created by Administrator on 2017/12/7.
 */
public class Appfragent extends SearchHeadFragment implements AppListContract.View<Datebean>, XRecyclerView.LoadingListener {

    private PageStateLayout mPageStatusLayout;
    private Button mBtnDetail;
    private XRecyclerView mRecyclerView;
    private CommonAdapter<ValueBean> mCommonAdapter;
    int pageIndex = 1;
    private AppListContract.Presenter mPresenter;
    private boolean refreshModelOrLoadmore = true;//默认为下拉刷新模式

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentview = inflater.inflate(R.layout.fragent_app, container, false);
        mPageStatusLayout = ((PageStateLayout) contentview.findViewById(R.id.statuslayout));
        Log.w("Homefragent", "onCreateView2");
        initThisView(contentview);
        return contentview;
    }

    private void initThisView(View view) {
        setSearchHeadView(view.findViewById(R.id.search));

        mRecyclerView = ((XRecyclerView) view.findViewById(R.id.recyclerview));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mCommonAdapter = new CommonAdapter<>(null);
        mRecyclerView.setAdapter(mCommonAdapter);
        mCommonAdapter.setItemViewType(Constances.ITEM_TYPE_APP_LIST);
        mRecyclerView.setLoadingListener(this);

        mPresenter = getPresenter();
        mPresenter.start();
        if (mPageStatusLayout != null) {
            mPageStatusLayout.onLoading();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void setPresenter(AppListContract.Presenter<Datebean> presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void showApplist(Datebean data) {
        if (mPageStatusLayout != null) {
            mPageStatusLayout.onSuccess();
        }
        if (mCommonAdapter != null) {
            if (refreshModelOrLoadmore) {
                mCommonAdapter.setData(data.getValue());
            } else {
                mCommonAdapter.addAll(data.getValue());
            }

        }
        mRecyclerView.refreshComplete();
    }

    @Override
    public void showNoMoreDataTips() {
        mRecyclerView.setLoadingMoreEnabled(false);
        Toast.makeText(getContext(), "无更多数据", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showloaderrotips(String erromsg) {
        Toast.makeText(getContext(), erromsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoadingTip() {
    }

    public AppListContract.Presenter getPresenter() {
        AppListContract.Presenter<Datebean> presenter = new TaskListPresenter<>(this, Constances.URL_LIST);
        HashMap<String, String> parms = new HashMap<>();
        parms.put("pageIndex", pageIndex + "");
        presenter.setParms(parms);
        return presenter;
    }

    @Override
    public void onRefresh() {
        mRecyclerView.setLoadingMoreEnabled(true);
        refreshModelOrLoadmore = true;
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                pageIndex = 1;
                HashMap<String, String> parms = new HashMap<>();
                parms.put("pageIndex", pageIndex + "");
                mPresenter.setParms(parms);
                mPresenter.start();
            }
        }, 1500);

    }

    @Override
    public void onLoadMore() {
        refreshModelOrLoadmore = false;
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter.getNextPageData();
                mRecyclerView.loadMoreComplete();
            }
        }, 1500);

    }
}
